<template>
  <div>
    <div>姓名:
      <el-input v-model="student.name" placeholder="请输入姓名"></el-input>
    </div>
    <div>年龄:
      <el-input v-model="student.age" placeholder="请输入年龄"></el-input>
    </div>
    <div>性别:
      <el-input v-model="student.gender" placeholder="请输入性别"></el-input>
    </div>
    <div>
      <el-button
        size="mini"
        type="success"
        @click="updateStudentDo">立即修改
      </el-button>
    </div>
  </div>
</template>

<script>
  export default {
    name: "addStudent",
    data() {
      return {
        queryId: null,
        student: {
          id: null,
          name: '',
          age: 0,
          gender: '男'
        }
      }
    },
    methods: {
      updateStudentDo() {
        let vueObj = this;
        //调用api的添加方法
        this.axios({
          headers: {
            'Content-Type': 'application/json'
          },
          method: "put",
          url: "http://192.168.32.146:8000/student/update",
          data: JSON.stringify(this.student)
        })
          .then(function (response) {
            // 跳转到学生列表
            if (response.data === 'ok') {
              vueObj.$router.push('/');
            }
          })
      }
    },
    mounted() {
      let id = this.$route.query.id
      this.queryId = id//修改查询第,提供后面修改方法使用
      //根据id查询,将数据渲染到页面
      let vueObj = this
      this.axios({
        method: "get",
        url: "http://192.168.32.146:8000/student/get/" + id,
      })
        .then(function (response) {
          console.log(response);
          vueObj.student = response.data
        })
    }
  }
</script>

<style scoped>

</style>
